package com.jiukuaidao.zhulin.module.address.presenter;

import com.jiukuaidao.library.net.MyRetroCallBack;
import com.jiukuaidao.zhulin.AppContext;
import com.jiukuaidao.zhulin.R;
import com.jiukuaidao.zhulin.api.ApiGetCityList;
import com.jiukuaidao.zhulin.data.TasksRepository;
import com.jiukuaidao.zhulin.entity.ApiCityListInfo;
import com.jiukuaidao.zhulin.module.address.contract.CityListContract;

/**
 * Author : PaiTaSuo
 * Date : 2016/6/13
 * Description :
 */
public class CityListPresenter implements CityListContract.Presenter {
    protected TasksRepository mRepository;
    protected CityListContract.View mView;

    public CityListPresenter(TasksRepository repository, CityListContract.View view) {
        this.mRepository = repository;
        this.mView = view;
    }

    @Override
    public void loadData() {
        mRepository.loadData(new ApiGetCityList<>(new MyRetroCallBack<ApiCityListInfo>() {
            @Override
            public void onSuccess(ApiCityListInfo result, String errorCode, String msg) {
                mView.cityList(result);
                if (mView.cityCode() == null) {
                    mView.setTitle(AppContext.getContext().getString(R.string.select_city));
                    return;
                }
                for (int i = 0; i < result.cityList.size(); i++) {
                    if (mView.cityCode().equals(result.cityList.get(i).cityCode)) {
                        mView.setTitle(result.cityList.get(i).cityName);
                        return;
                    } else {
                        mView.setTitle(AppContext.getContext().getString(R.string.select_city));
                    }
                }
            }

            @Override
            public void onError(Throwable t, int resCode, String msg) {
                mView.toast(msg);

            }

            @Override
            public void onFail(ApiCityListInfo result, String errorCode, String msg) {
                mView.toast(msg);
            }
        }));
    }
}
